package cn.kgc.shiro03.controller;

import cn.kgc.shiro03.dto.UserDTO;
import cn.kgc.shiro03.dto.UserPageDTO;
import cn.kgc.shiro03.entity.User;
import cn.kgc.shiro03.service.UserService;
import cn.kgc.shiro03.util.CodeMsg;
import cn.kgc.shiro03.util.ResponseResult;
import cn.kgc.shiro03.vo.UserVO;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.SessionAttribute;

import java.util.Map;

/**
 * @author: mosin
 * @version: v1.0  2022/11/21
 */
@RestController
@RequestMapping("user")
public class UserController {

    @Autowired
    private UserService userService;

    // 分页查询   查询所有   搜索
    @RequestMapping("all")
    public ResponseResult selectUserPage(UserPageDTO userPageDTO) {

        ResponseResult responseResult = userService.selectPage(userPageDTO);

        return responseResult;
    }

    @RequestMapping("updateState")
    public ResponseResult updateUserState(User user) {

        ResponseResult responseResult = userService.updateUserState(user);

        return responseResult;
    }

    @RequestMapping("add")
    public ResponseResult addUser(@RequestBody UserVO userVO) {

        ResponseResult responseResult = userService.addUser(userVO);


        return responseResult;
    }

    // 根据id 查询用户信息
    @RequestMapping("getById")
    public ResponseResult getUserById(Integer id) {

        ResponseResult responseResult = userService.getUserVOById(id);
        return responseResult;
    }

    @RequiresRoles({"super"})
    @RequestMapping("update")
    public ResponseResult updateUser(@RequestBody UserVO userVO) {

        ResponseResult responseResult = userService.updateUser(userVO);
        return responseResult;
    }

    @RequiresPermissions({"user:delete"})
    @RequestMapping("delete")
    public ResponseResult deleteUser(String ids) {

        ResponseResult responseResult = userService.deleteUser(ids);
        return responseResult;
    }


    @RequestMapping("loadMenu")
    public Map<String, Object> loadMenu(String username) {
        Map<String, Object> map = userService.loadMenu(username);
        return map;
    }

    @RequestMapping("login")
    public ResponseResult login(UserDTO userDTO,@SessionAttribute("code") String code) {

        System.out.println("userDTO = " + userDTO);
        Subject subject = SecurityUtils.getSubject();
        UsernamePasswordToken token = new UsernamePasswordToken(userDTO.getUsername(), userDTO.getPassword());

         if("true".equals(userDTO.getRememberMe())){ // 开启记住我的功能
             token.setRememberMe(true);
         }

        System.out.println(" session code = " + code);
        ResponseResult responseResult = new ResponseResult();

        if(code.equals(userDTO.getCaptcha())){
            subject.login(token);
            responseResult.setMsg(CodeMsg.LOGIN_SUCCESS.getMsg());
            responseResult.setCode(CodeMsg.LOGIN_SUCCESS.getCode());
        }else{
            responseResult.setMsg(CodeMsg.CODE_FAIL.getMsg());
            responseResult.setCode(CodeMsg.CODE_FAIL.getCode());
        }
        return  responseResult;
    }


    @RequestMapping("logOut")
    public ResponseResult  logOut(){

        Subject subject = SecurityUtils.getSubject();
        subject.logout();

        ResponseResult responseResult = new ResponseResult();
        responseResult.setMsg(CodeMsg.LOGIN_OUT.getMsg());
        responseResult.setCode(CodeMsg.LOGIN_OUT.getCode());
        return responseResult;
    }

}
